import { defineConfig,loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import styleImport, { VantResolve } from 'vite-plugin-style-import';

const path = require('path');
//http://10.124.131.20:8081/view/ar-h5/job/h5-migu-nba-exchange-web_test/2/console


// https://vitejs.dev/config/
export default defineConfig( ({ command, mode })=>{
    const env = loadEnv(mode, process.cwd(), '')  // 读取环境变量

    const appEnv = env.VITE_ENV
    console.log('appEnv', appEnv)

    const configMap = {
      development: {
        cdn: '//10.46.235.126/static_test'
      },
      production: {
        cdn: '//arss.migudm.cn/static'
      }
    }

    // 发布模式
    const isPublish = process.argv.indexOf('build') > -1

    const {cdn} = configMap[appEnv]
    // 应用路径
    const appPath = `/spa/nba-exchange/`

    const publicPath = isPublish ? `${cdn}/webar/projects${appPath}` : appPath

    const outputDir = isPublish ? 'dist/H5CDN/webar/projects' + appPath : 'dist'

    return {
      plugins: [
        vue(),
        styleImport({
          resolves: [VantResolve()],
        })
      ],
      base: './',
      // build: {
      //   // 指定输出路径(相对与根目录)
      //   outDir: outputDir
      // },
      resolve:{
        alias:{
          "@":path.resolve(__dirname,'./src')
        }
      },
      // publicDir: publicPath,
      // define:{
      //   'process.env':process.env
      // },
      // 本地运行配置，及反向代理配置
      server: {
          host: "0.0.0.0", // 可以使用ip访问，不设置则只能用localhost访问
          //cors: true, // 默认启用并允许任何源
          //open: true, // 在服务器启动时自动在浏览器中打开应用程序
          //反向代理配置，注意rewrite写法，开始没看文档在这里踩了坑
          // proxy: {
          //   '/api': {
          //     target:env.VITE_APP_API_SEARVER, 
          //     changeOrigin: true,
          //     ogLevel:'debug',
          //     rewrite: (path) => path.replace(/^\/api/, '')
          //   }
          // }
      }
    }
  })
